package com.macro.controller;

import com.macro.entity.BasStore;
import com.macro.entity.BasWareHouse;
import com.macro.entity.SysUser;
import com.macro.service.SysUserService;
import com.macro.utils.AjaxResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户信息
 * Controller   返回客户端数据
 */
@RestController
@RequestMapping("/api/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private HttpSession session;

    /**
     * 根据用户名和密码查询用户信息及权限信息
     * @param uname     用户名
     * @param upwd      密码
     * @return
     */
    @GetMapping
    public AjaxResponse queryUserByNameAndPwd(String uname, String upwd){
        String isdisable=sysUserService.findUserByNameAndPwdCount(uname,upwd);
        if(isdisable.equals("0")){
            SysUser sysUser = (SysUser) sysUserService.findUserByNameAndPwd(uname,upwd);
            session.setAttribute("USER",sysUser);
            return AjaxResponse.success(200,sysUser);
        }else if(isdisable.equals("1")){
            return AjaxResponse.success(300,"抱歉，该用户已经被禁用！");
        }else if(isdisable.equals("2")){
            return AjaxResponse.success(404,"抱歉，没有找到该用户！");
        }else{
            return null;
        }
    }

    /**
     * 获取用户Session值
     * @return
     */
    @GetMapping("session")
    public AjaxResponse querySessionUser(){
        //判断用户信息是否为空
        if(session.getAttribute("USER")!=null){
            return AjaxResponse.success((SysUser) session.getAttribute("USER"));
        }else{
            return AjaxResponse.success(404,"用户信息Session失效！");
        }
    }

    /**
     * 用户退出登录/删除Session记录
     * @return
     */
    @GetMapping("loginout")
    public Map<String,Object> userLoginOut(){
        session.removeAttribute("USER");
        return AjaxResponse.success("已退出登录！");
    }

    /**
     * 是否有权限访问仓库信息
     * @return
     */
    @GetMapping("userhouse")
    public AjaxResponse userSysRoleHouse(Integer houseid){
        if(session.getAttribute("USER")!=null){
            SysUser sysUser = (SysUser) session.getAttribute("USER");
            Integer i=1;
            for (BasWareHouse wareHouse : sysUser.getFindWareHouseList()) {
                if(houseid.toString().equals(wareHouse.getHouseid().toString())){
                    return AjaxResponse.success(wareHouse);
                }
                if(i==sysUser.getFindStoreList().size()){
                    return AjaxResponse.nofind("抱歉，你没有权限访问该仓库信息！");
                }
                i++;
            }
            return AjaxResponse.nofind("");
        }else{
            return AjaxResponse.nofind("抱歉，用户信息Session失效！");
        }
    }

    /**
     * 是否有权限访问店铺信息
     * @return
     */
    @GetMapping("userstore")
    public Map<String,Object> userSysRoleStore(Integer storeid){
        if(session.getAttribute("USER")!=null){
            SysUser sysUser = (SysUser) session.getAttribute("USER");
            Integer i=1;
            for (BasStore basStore : sysUser.getFindStoreList()) {
                if(storeid.toString().equals(basStore.getStoreid().toString())){
                    return AjaxResponse.success(basStore);
                }
                if(i==sysUser.getFindStoreList().size()){
                    return AjaxResponse.nofind("抱歉，你没有权限访问该店铺信息！");
                }
                i++;
            }
            return AjaxResponse.nofind("");
        }else{
            return AjaxResponse.nofind("抱歉，用户信息Session失效！");
        }
    }
}
